﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_Default : System.Web.UI.Page
{
    public static string str;
    public static string id;
    public static string title;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            //绑定ddlBigType控件
            this.ddlBigTypeDataBind();
            //在gvType中显示tb_Type表中与ddlBigType控件中的第一条记录相匹配的记录
            gvTypeDataBind();
            this.Label1.Text = DB.TYPE_SECTION;
        }
    }
    //数据绑定(ddlBigType)
    public void ddlBigTypeDataBind()
    {
        DataTable dt1 = DB.ExcuteDataTable("SELECT * FROM tb_BigType WHERE Type='物资大类'");
        if (dt1.Rows.Count == 0)
        {
            DB.TYPE_SECTION = "物资大类";
            Response.Write("<script>alert('尚无可选择的物资大类，请先设置！');location='EditBigType.aspx';</script>");
        }
        else
        {
            ddlBigType.DataSource = dt1;
            ddlBigType.DataTextField = "Title";
            ddlBigType.DataValueField = "ID";
            ddlBigType.DataBind();
            //使用ViewState来保存数据表dt1中第一条记录的Title的值
            ViewState["TYPE"] = dt1.Rows[0]["Title"].ToString();
        }
    }

    //数据绑定(gvType)
    public void gvTypeDataBind()
    {
        DataTable dt2 = DB.ExcuteDataTable("select ID, Title from tb_Type where Type='" + ViewState["TYPE"] + "'");
        gvType.DataSource = dt2;
        gvType.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        //以下三行代码是自动生成 编码序号(Sequence)
        DataSet ds1 = DB.reDs("SELECT Max(Sequence) FROM tb_Type WHERE Type='" + ViewState["TYPE"] + "'");
        string strSequence = ds1.Tables[0].Rows[0][0].ToString();
        int sequence = 0;
        if (strSequence == "")
        {
            DataTable dt=DB.ExcuteDataTable("Select sequence from tb_BigType where Title='" + ViewState["TYPE"] + "'");
            sequence = Convert.ToInt32(dt.Rows[0][0].ToString() +"1") ;
        }
        else
        {
            sequence = Convert.ToInt32(strSequence.Substring(0, 2) + Convert.ToString(Convert.ToInt32(strSequence.Substring(2, strSequence.Length - 2)) + 1));

        }
        string cmdText = "INSERT tb_Type(Sequence,Title,Type) VALUES('" + sequence + "','" + txtYype.Text.Trim() + "','" + ViewState["TYPE"] + "')";
        DB.ExSql(cmdText);
        gvTypeDataBind();
        this.txtYype.Text = "";
    }

    //为gvType分页
    protected void gvType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //设置GridView当前页的索引
        this.gvType.PageIndex = e.NewPageIndex;
        //重新绑定数据库
        gvTypeDataBind();
    }
    protected void ddlBigType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["TYPE"] =this.ddlBigType.SelectedItem.ToString ();
        gvTypeDataBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        bool b=DB.ExSql("DELETE FROM tb_Type WHERE ID=" + id);
        gvTypeDataBind();
        if(b)
            DB.ExSql("DELETE FROM tb_Material WHERE WzType='" + title + "'");
    }

    protected void lnkButtonSelect_Command(object sender, CommandEventArgs e)
    {
        //获取lnkButtonDelete的LinkButton对象
        LinkButton lbtn = (LinkButton)sender;
        //引用lnkButtonDelete控件的父控件上一级控件
        GridViewRow row = (GridViewRow)lbtn.Parent.Parent;
        //取得该父控件的Text值
        id = ((DataControlFieldCell)gvType.Rows[row.RowIndex].Cells[0]).Text.Trim();
        title =((DataControlFieldCell)gvType.Rows[row.RowIndex].Cells[1]).Text.Trim();
        DataTable dt = DB.ExcuteDataTable("select count(*) from tb_Material where WZType='" + title + "'");
        string countType = dt.Rows[0][0].ToString();
        if (countType != "0")
        {
            str = "你删除项包含物资信息，若删除此项，则将删除该物资类别的物资信息。";
        }
        else
        {
            str = "";
        }
        str += "你确认要删除此选项吗？";
        //实现单击【删除】按钮时，弹出警告信息
        btnDelete.Attributes["onclick"] = "javascript:return confirm('" + str + "')";
    }
}
